import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Demo5 {
    static Scanner scanner = new Scanner(System.in);
    public static int outTrue(){
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
           arr[i] = scanner.nextInt();
        }
        int count = 0;
        int cur = 0;
        int now = arr[0];
        while (cur < n-1){
            HashMap<Integer,Integer> hashMapMin = new HashMap<>();
            HashMap<Integer,Integer> hashMapMax = new HashMap<>();
            int minMax = 0;
            int maxMin = 101;
            for (int i = cur+1; i < k + cur + 1 && i < n; i++) {
                if(arr[cur] >= arr[i]){
                    hashMapMin.put(arr[i],i);
                    if (arr[i] >= minMax){
                        minMax = arr[i];
                    }
                }else {
                    hashMapMax.put(arr[i],i);
                    if (arr[i] < maxMin){
                        maxMin = arr[i];
                    }
                }
            }
            if (!hashMapMin.isEmpty()){
                now = minMax;
                cur = hashMapMin.get(minMax);
            }else {
                cur = hashMapMax.get(maxMin);
                count = maxMin - now + count;
                now = maxMin;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(outTrue());
    }
}
